#!/bin/bash
#部署系统环境
#v2.1

# 版本检查
check_name(){
    echo -e "\033[31m =====检查系统版本..===== \033[0m"
    [ -f /etc/redhat-release ] || exit 1
    System=`cat /etc/redhat-release |awk '{print $1}'`
    version=`cat /etc/redhat-release |awk '{print $4}'`
    if [[ ${System} == 'CentOS' ]];then
        if [[ ${version} ==  '7.6.1810' ]];then
            echo 'Version checking' ${System} ${version}
        fi
    else
            echo 目前版本: 'Version checking' ${System} ${version}
            echo 建议版本： 'Version checking' CentOS 7.6.1810
    fi
}


##网络监测
net_check(){
    echo -e "\033[31m =====正在监测网络连通性...===== \033[0m"
    ping -c1 -W1 www.baidu.com &> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[31m =====检测当前网络正常，为您配置在线yum源...===== \033[0m"
    else
        exit 1
    fi
}


##修改yum仓库
yum_init(){
    echo -e "\033[31m =====配置yum源...===== \033[0m"
    mkdir -p /etc/yum.repos.d/repo.bak
    mv -f /etc/yum.repos.d/* /etc/yum.repos.d/repo.bak &> /dev/null
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &> /dev/null
    curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo &> /dev/null
    curl -o /etc/yum.repos.d/docker-ce.repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo &> /dev/null
    yum clean all &> /dev/null
    yum makecache &> /dev/null
    echo -e "\033[31m =====在线源已配置完成...===== \033[0m"
}

##系统初始化
sys_init(){
    echo -e "\033[31m =====系统初始化...===== \033[0m"
    yum -y install bash-completion net-tools telnet wget net-tools curl chrony unzip &> /dev/null
    sed -ir '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
    echo "SELINUX=disabled" >> /etc/selinux/config
    #sed -i "s/#Port 22/Port 30022/g" /etc/ssh/sshd_config
    sed -i 's#\&\& chmod  600 /tmp/ -R##g' /var/spool/cron/root &> /dev/null
    sed -i 's/^#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
    sed -i 's/^#UseDNS yes/UseDNS no/g' /etc/ssh/sshd_config
    sed -i "s/GSSAPIAuthentication yes/GSSAPIAuthentication no/g" /etc/ssh/sshd_config
    sed -i -e '49s/^#//g' /etc/ssh/sshd_config
    cat /dev/null > /etc/sysconfig/i18n
    echo 'LANG="en_US.UTF-8"' >> /etc/sysconfig/i18n
    echo 'LANG="zh_CN.UTF-8"' > /etc/locale.conf
    echo 'SYSFONT="latarcyrheb-sun16"' >> /etc/sysconfig/i18n
    mv /etc/localtime /etc/localtime.bak
    cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    source /etc/locale.conf
    setenforce 0 &> /dev/null
    ulimit -n 1024000 2>/dev/null
    rm -f /etc/security/limits.d/90-nproc.conf
    > /etc/security/limits.conf
    > /etc/security/limits.d/20-nproc.conf
    echo "kernel.pid_max=1000000" >> /etc/sysctl.conf
    echo "alias cdPath='cd /data/docker-compose'" >> /etc/profile
    echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/profile
    timedatectl set-timezone Asia/Shanghai
    timedatectl set-local-rtc 1
    chronyc sources
    systemctl stop firewalld.service &> /dev/null && systemctl disable firewalld.service &> /dev/null
    systemctl stop iptables.service &> /dev/null && systemctl disable iptables.service &> /dev/null

cat > /etc/security/limits.conf <<-eof
* soft nofile 1024000
* hard nofile 1024000
* soft nproc 1024000
* hard nproc 1024000
* soft nofile 1024000
* hard nofile 1024000
* soft memlock unlimited
* hard memlock unlimited
eof

cat > /etc/security/limits.d/20-nproc.conf<<-eof
* soft nproc 1024000
* hard nproc 1024000
eof
    sysctl -p
    mkdir -p /data/
    echo -e "\033[31m =====系统初始化完成...===== \033[0m"
}

##安装docker-ce
install_docker(){
    echo -e "\033[31m =====Docker初始化...===== \033[0m"
    #安装docker-ce
    yum install -y docker-ce docker-ce-cli docker-compose.noarch containerd.io &> /dev/null
    mkdir -p /etc/docker/
    cat >> /etc/docker/daemon.json <<EOF
    {"exec-opts": ["native.cgroupdriver=systemd"]} 
    {
     "registry-mirrors": ["https://mirror.ccs.tencentyun.com"] 
    }
EOF
    systemctl daemon-reload && systemctl enable docker && systemctl restart docker
    pid=`ps -ef |grep -v grep |grep docker |wc -l`
    if [ $pid -eq 1 ];then
    echo -e "\033[31m =====docker初始化成功...===== \033[0m"
        systemctl restart docker
    else
        echo -e "\033[31m =====docker初始化失败...===== \033[0m"
    fi
}

##安装docker-compose
install_docker_compose(){
    echo -e "\033[31m =====Docker compose初始化...===== \033[0m"
    #[ -f /usr/bin/docker-compose ] || wget -P /usr/bin/ http://10.12.70.232/resource/oss/init/tools/docker-compose  &> /dev/null && chmod +x /usr/bin/docker-compose
    [ -f /usr/bin/docker-compose ] || curl -L https://get.daocloud.io/docker/compose/releases/download/v2.11.2/docker-compose-`uname -s`-`uname -m` > /usr/bin/docker-compose && chmod +x /usr/bin/docker-compose
    if [ $? -eq 0 ];then
        echo -e "\033[31m =====docker-compose初始化成功...===== \033[0m"
    else
        echo -e "\033[31m =====docker-compose初始化失败...===== \033[0m"
    fi
}

##登录docker仓库
login_docker(){
    echo -e "\033[31m =====正在登录仓库...===== \033[0m"
    docker login --username=内蒙古己度科技有限公司 registry.cn-beijing.aliyuncs.com --password='Q1w2e3r4t5' &> /dev/null
    if [ $? -eq 0 ];then
        echo -e "\033[31m =====登录成功===== \033[0m"
    else
        echo -e "\033[31m =====登录失败===== \033[0m"
    fi
}


main (){
check_name
net_check
yum_init
sys_init
install_docker
install_docker_compose
login_docker
echo -e "\033[31m =====done===== \033[0m"
exit 0
}

echo "部署中...日志详见output.log "
main

# 删除部署脚本
sleep 5
rm -rf /root/deploy.sh
echo "ok"
